package com.bwhx.eruler.controller.casdilly;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Staff;
import com.bwhx.eruler.bean.casdilly.AlertRuler;
import com.bwhx.eruler.bean.casdilly.ClothesType;
import com.bwhx.eruler.bean.casdilly.ClothesTypeModelData;
import com.bwhx.eruler.controller.BaseController;
import com.bwhx.eruler.service.casdilly.ClothesTypeService;
import com.bwhx.eruler.util.Constant;
import com.bwhx.eruler.util.StringUtils;

@Controller
@RequestMapping("/clothesType")
public class ClothesTypeController extends BaseController {

	@Autowired
	ClothesTypeService clothesTypeService;

	@ResponseBody
	@RequestMapping("/findByTypeCPIDSerial.ashx")
	public HashMap<String, Object> findByTypeCPIDSerial(HttpServletRequest request) {
		HashMap<String, Object> resultMap = new HashMap<String, Object>();

		try {
			List<ClothesType> clothesTypeList = clothesTypeService
					.findByTypeCPIDSerial((String) request.getAttribute("cPIDSerial"));
			resultMap.put(Constant.DATA, clothesTypeList);
			resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
			resultMap.put(Constant.MSG, Constant.MSG_SUCCESS);
		} catch (Exception e) {
			log.error(getTrace(e));
			resultMap.put(Constant.STATUS, Constant.STATUS_FAIL);
			resultMap.put(Constant.MSG, Constant.MSG_FAIL);
		}

		return resultMap;
	}

	@ResponseBody
	@RequestMapping("/page.findByTypeCPIDSerial.ashx")
	public List<ClothesType> findByTypeCPIDSerial2(String typeCPIDSerial) {
		List<ClothesType> clothesTypeList = null;
		try {
			if (typeCPIDSerial != null && !typeCPIDSerial.isEmpty()) {
				clothesTypeList = clothesTypeService.findByTypeCPIDSerial(typeCPIDSerial);
			}
		} catch (Exception e) {
			clothesTypeList = null;
		}
		return clothesTypeList;

	}

	@ResponseBody
	@RequestMapping("/page.saveDisplayOrder.ashx")
	public Map<String, String> saveDisplayOrder(HttpServletRequest request, String data) {
		HashMap<String, String> result = new HashMap<String, String>();
		List<ClothesType> typies = new ArrayList<ClothesType>();
		if (data != null && !data.isEmpty()) {
			String[] array = data.split(",");
			String[] type = null;
			if (array != null && array.length > 0) {
				for(int i=0;i<array.length;i++) {
					type = array[i].split(":");
					if(type != null && type.length>0) {
						ClothesType clothesType = new ClothesType();
						clothesType.setTypeID(type[0]);
						clothesType.setDisplayOrder(StringUtils.tryParseInt(type[1]));
						typies.add(clothesType);
					}
				}
				if(typies.size()>0) {
					if(clothesTypeService.saveDisplayOrder(typies)) {
						result.put(Constant.STATUS, Constant.STATUS_SUCCESS);
						result.put(Constant.MSG, Constant.MSG_FAIL);
					}else {
						result.put(Constant.STATUS, Constant.STATUS_FAIL);
						result.put(Constant.MSG, Constant.MSG_FAIL);	
					}
				}else {
					result.put(Constant.STATUS, Constant.STATUS_FAIL);
					result.put(Constant.MSG, Constant.MSG_FAIL);		
				}
			} else {
				result.put(Constant.STATUS, Constant.STATUS_FAIL);
				result.put(Constant.MSG, Constant.MSG_FAIL);
			}
		} else {
			result.put(Constant.STATUS, Constant.STATUS_FAIL);
			result.put(Constant.MSG, Constant.MSG_FAIL);
		}
		return result;
	}

	@ResponseBody
	@RequestMapping("/page.findByTypeCategory.ashx")
	public List<ClothesType> findByTypeCategory(Integer typeCategory) {

		List<ClothesType> clothesTypeList = clothesTypeService.findByTypeCategory(typeCategory);

		return clothesTypeList;

	}

	@ResponseBody
	@RequestMapping(value = "/page.saveOrUpdate.ashx")
	public String saveOrUpdate(ClothesType ct, HttpSession session) {
		try {
			Staff staff = (Staff) session.getAttribute("staff");

			String[] itemArray = ct.getTypeItemsName().split(",");
			List<String> itemList = Arrays.asList(itemArray);
			List<String> tempList = new ArrayList<String>();

			Iterator<String> it = itemList.iterator();
			while (it.hasNext()) {
				String a = it.next();
				if (tempList.contains(a)) {
					return "fail_" + a;
				}
			}

			ClothesType clothesType = new ClothesType();
			clothesType.setTypeCPIDSerial(ct.getTypeCPIDSerial());
			clothesType.setTypeCategory(ct.getTypeCategory());
			clothesType.setOldTypeName(ct.getOldTypeName());
			/*Page page = new Page();
			page.setRows(0);
			page.setSearchObj(clothesType);
			List<ClothesType> list = clothesTypeService.list(page);

			/*
			 * if(ct.getTypeCategory()==1 && list.size()>0){
			 * if(ct.getTypeID().equals("-1")){ return "onlyone"; }else{ if(list.size()>1 ||
			 * !(list.get(0).getTypeID().equals(ct.getTypeID()))){ return "onlyone"; } }
			 * 
			 * }
			 */

			if (ct.getTypeID().equals("-1")) {

				// 首先查重
				List<ClothesType> resultCTList = clothesTypeService.findByCTNameAndTypeAndProjectSerial(ct);
				if (resultCTList.size() != 0) {
					return "nameconflict";
				}

				ct.setCreateTime(new Date());
				ct.setCreatorID(staff.getsId().toString());

				clothesTypeService.insert(ct);
			} else {

				// 首先查重
				/*List<ClothesType> resultCTList = clothesTypeService.findByCTNameAndTypeAndProjectSerial(ct);
				if (resultCTList.size() > 1
						|| (resultCTList.size() == 1 && !(resultCTList.get(0).getTypeID().equals(ct.getTypeID())))) {

					return "nameconflict";
				}*/

				ct.setModifierID(staff.getsId().toString());
				ct.setModifyTime(new Date());
				clothesTypeService.update(ct);
			}

		} catch (Exception e) {
			log.error(getTrace(e));
			return "fail";
		}

		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/page.list.ashx")
	public Map<String, Object> ctList(Integer page, Integer rows, ClothesType ct, HttpSession session) {

		Staff staff = (Staff) session.getAttribute("staff");
		if (null == staff.getUserType() || 1 != staff.getUserType()) {
			ct.setCreatorID(staff.getsId().toString());

		}

		// 每页的开始记录
		int start = (page - 1) * rows;
		Page p = new Page(start, rows, ct);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			List<ClothesType> list = clothesTypeService.list(p);
			Integer count = clothesTypeService.count(ct);

			resultMap.put("total", count);// total键 存放总记录数，必须的
			resultMap.put("rows", list);// rows键 存放每页记录 list

		} catch (Exception e) {
			log.error(getTrace(e));
			resultMap.put("msg", "失败！");
			resultMap.put("status", "0");
		}

		return resultMap;
	}

	@ResponseBody
	@RequestMapping(value = "/page.del.ashx")
	public Map<String, String> OfflineOrderDel(ClothesType ct) {
		Map<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("status", "1");
		Staff staff = this.getSessionStaff();
		ct.setModifierID(staff.getsId().toString());
		ct.setModifyTime(new Date());
		List<ClothesType> typies = clothesTypeService.findByCTNameAndTypeAndProjectSerial(ct);
		if (typies != null && typies.size() > 0) {
			StringBuilder dataStr = new StringBuilder();
			for (int i = 0; i < typies.size(); i++) {
				dataStr.append(typies.get(i).getTypeID());
				dataStr.append(",");
			}
			dataStr.deleteCharAt(dataStr.length() - 1);
			ct.setTypeID(dataStr.toString());
		} else {
			ct.setTypeID("0");
		}
		clothesTypeService.delete(ct);

		return resultMap;
	}

	@ResponseBody
	@RequestMapping(value = "/page.listAllPublic.ashx")
	public List<ClothesType> listAll() {
		// 每页的开始记录
		int start = 0;
		ClothesType ct = new ClothesType();
		ct.setTypeCategory(0);
		Page p = new Page(start, 0, ct);
		List<ClothesType> list = clothesTypeService.list(p);
		return list;
	}

	@ResponseBody
	@RequestMapping(value = "/page.findByID.ashx")
	public ClothesType findByID(ClothesType ct) {

		return clothesTypeService.findByID(ct);
	}

	@ResponseBody
	@RequestMapping("/page.relateClothes.ashx")
	public String relateClothes(ClothesType ct) {
		Staff staff = this.getSessionStaff();
		ct.setModifierID(staff.getsId().toString());
		ct.setModifyTime(new Date());

		clothesTypeService.relateClothes(ct);
		return "success";

	}

}
